//
//  NewsTableViewCell.m
//  SDSY
//
//  Created by dale on 2017/3/24.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "NewsTableViewCell.h"
#import "SDPhotoBrowser.h"
@interface NewsTableViewCell()<SDPhotoBrowserDelegate>

@end

@implementation NewsTableViewCell{
    UIImageView* _iconImageView;//图标
    UILabel*     _topicLB;//主题
    UILabel*     _dateLB;//发布日期
    UILabel*     _contentLB;//内容
    UIView*      _bottomView;
    NSMutableArray *imageArray;
}



// 自定义初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}


-(void)setupView{
    //init
    _iconImageView=[[UIImageView alloc] init];
    [self.contentView addSubview:_iconImageView];
    
    _topicLB=[[UILabel alloc] init];
    
    [self.contentView addSubview:_topicLB];
    
    _dateLB=[[UILabel alloc] init];
    [self.contentView addSubview:_dateLB];
    
    _contentLB=[[UILabel alloc] init];
    [self.contentView addSubview:_contentLB];
    
    _bottomView=[[UIView alloc] init];
    [self.contentView addSubview:_bottomView];

    // 底部图片组
//    self.photosGroup = [[SDPhotoGroup alloc] init];
//    [self.contentView addSubview:self.photosGroup];
    
    //layout
    _iconImageView.sd_layout.leftSpaceToView(self.contentView,5).topSpaceToView(self.contentView,5).widthIs(40).heightIs(40);
    
    _topicLB.sd_layout.leftSpaceToView(_iconImageView,5).topSpaceToView(self.contentView,5).heightIs(20);
    [_topicLB setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH - 60];
    
    _dateLB.sd_layout.leftSpaceToView(_iconImageView,5).topSpaceToView(_topicLB,5).widthIs(150).heightIs(15);
    
    _contentLB.sd_layout.leftSpaceToView(self.contentView,5).topSpaceToView(_iconImageView,10).rightSpaceToView(self.contentView,5).autoHeightRatio(0);
    
    //设定时间字体大小及颜色
    _dateLB.font=[UIFont systemFontOfSize:12];
    _dateLB.textColor=[UIColor grayColor];
    
    _topicLB.font = [UIFont systemFontOfSize:15];


    
}


-(void)setNewsmodel:(NewsModel *)newsmodel{
    _newsmodel=newsmodel;

        // 判断是哪一个模块发出的新闻,取出对应的图标和文字
    if (newsmodel.college_id == 0) {
        // 显示公告icon
        _iconImageView.image=[UIImage imageNamed:@"notice"];
    }else {
        // 检查排除数组越界的错误
        if (newsmodel.college_id-2 < 0) {
            [SVProgressHUD showfailed];
            return;
        }
        // 显示博雅icon
       _iconImageView.image=[UIImage imageNamed:CLASSIFY_ICON[newsmodel.college_id-3]];
    }
    
    _topicLB.text=newsmodel.name;

    _dateLB.text=newsmodel.endtime;
    _contentLB.text=newsmodel.act_news;


    if(newsmodel.img_url==nil||newsmodel.img_url.count==0){
        [self setupAutoHeightWithBottomView:_contentLB bottomMargin:5];
    }else {
        // 设置一排固定间距自动宽度子view
        [self setupAutoWidthViewsWithCount:newsmodel.img_url.count margin:2];
        [self setupAutoHeightWithBottomView:_bottomView bottomMargin:5];

    }
    
    
}

// 设置一排固定间距自动宽度子view
- (void)setupAutoWidthViewsWithCount:(NSInteger)count margin:(CGFloat)margin
{
    _bottomView = [UIView new];
    [self.contentView addSubview:_bottomView];

    imageArray = [[NSMutableArray alloc] init];


    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < count; i++) {
        UIImageView *imageView = [UIImageView new];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.newsmodel.img_url[i]]placeholderImage:[UIImage imageNamed:@"Error"]];
        [_bottomView addSubview:imageView];
        imageView.sd_layout.autoHeightRatio(1); // 设置高度约束
        [temp addObject:imageView];

        // 图片URL
        imageView.tag = i;
        [imageArray addObject:imageView];

        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = true;
    }

    _bottomView.sd_layout
    .leftSpaceToView(self.contentView, 0)
    .rightSpaceToView(self.contentView, 0)
    .topSpaceToView(_contentLB, 10);

    // 此步设置之后_autoWidthViewsContainer的高度可以根据子view自适应
    [_bottomView setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:3 verticalMargin:margin horizontalMargin:margin verticalEdgeInset:5 horizontalEdgeInset:10];

}

#pragma mark - 点击事件
-(void)imageTapped:(UITapGestureRecognizer *)sender {
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = _bottomView; // 原图的父控件
    browser.imageCount = self.newsmodel.img_url.count; // 图片总数
    browser.currentImageIndex = sender.view.tag;
    browser.delegate = self;
    [browser show];
}

#pragma mark - SDPhotoBrowserDelegate协议
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    UIImageView *imageView = imageArray[index];
    return imageView.image;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    return [NSURL URLWithString:self.newsmodel.img_url[index]];
}



@end
